/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of MyTricTrac.
 *
 * MyTricTrac is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyTricTrac is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with My Accounts.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.montrictracv4.db.dao;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.montrictracv4.ApplicationConstants.DATA_TYPE;
import org.amphiprion.montrictracv4.db.model.UserGameData;

import android.content.Context;
import android.database.Cursor;

/**
 * This class is responsible of all database collection access.
 * 
 * @author amphiprion
 * 
 */
public class UserInfoDao extends AbstractDao {
	/** The singleton. */
	private static UserInfoDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private UserInfoDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static UserInfoDao getInstance(Context context) {
		if (instance == null) {
			instance = new UserInfoDao(context);
		}
		return instance;
	}

	public void persit(UserGameData data) {
		if (!isInDb(data.dataId)) {
			String sql = "insert into USER_INFO (id, name, data_type) values (?,?,?)";
			Object[] params = new Object[3];
			params[0] = data.dataId;
			params[1] = data.nameFromData;
			params[2] = data.typeForData.ordinal();

			execSQL(sql, params);
		}

		String sql = "insert into USER_INFO_GAME (id_game, id_user_info, value) values (?,?,?)";
		Object[] params = new Object[3];
		params[0] = data.gameId;
		params[1] = data.dataId;
		params[2] = data.value;

		execSQL(sql, params);

	}

	public boolean isInDb(String id) {
		boolean result = false;
		String sql = "SELECT count(*) from USER_INFO where id=?";
		Cursor cursor = getDatabase().rawQuery(sql, new String[] { id });
		cursor.moveToFirst();
		result = cursor.getInt(0) != 0;
		cursor.close();
		return result;
	}

	public List<UserGameData> getAll(boolean excludeStringType) {
		String sql = "SELECT id, name, data_type FROM USER_INFO";
		if (excludeStringType) {
			sql += " where data_type !=" + DATA_TYPE.STRING.ordinal();
		}
		sql += " order by name COLLATE NOCASE";

		Cursor cursor = getDatabase().rawQuery(sql, new String[] {});
		ArrayList<UserGameData> result = new ArrayList<UserGameData>();
		if (cursor.moveToFirst()) {
			do {
				UserGameData entity = new UserGameData("", cursor.getString(0));
				entity.nameFromData = cursor.getString(1);
				entity.typeForData = DATA_TYPE.values()[cursor.getInt(2)];
				entity._table = "USER_INFO";
				entity._tableJoin = "USER_INFO_GAME";
				entity._tableJoinFk = "id_user_info";
				result.add(entity);

			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}
}
